import trinity
import blue
import nodemanager
import timecurves
import random
import uthread
import log
import base
import util

class StripMiningTurret():

    def __init__(self, graphicURL):
        model = blue.os.LoadObject(graphicURL)
        self.dummy_dest = model.children[0].children[0].children[1]
        self.source_placement = model.children[0].children[0].children[2]
        self.dest_placement = model.children[0].children[0].children[3]
        self.beam_placement = model.children[0].children[0].children[4]
        self.model = model
        self.soundNode = model.children[1]
        self.primed = 0
        self.vxOwners = None
        self.dest_placement.display = 0
        self.beam_placement.display = 0



    def Prime(self):
        if (self.primed != 0):
            return 
        self.curves = timecurves.ReadCurves(self.model.children[0])



    def Release(self):
        self.Rest()
        self.source_placement = None
        self.dest_placement = None
        self.beam_placement = None
        self.model = None
        self.soundNode = None



    def SetTarget(self, shipID, targetID):
        if (self.primed == 0):
            self.Prime()
        targetBall = sm.GetService('michelle').GetBall(targetID)
        self.targetOwner = targetBall
        if ((targetBall is None) or (getattr(targetBall, 'model', None) is None)):
            self.Rest()
            return 
        self.SetSubTarget(shipID, targetID)



    def Shoot(self):
        for curve in self.curves:
            curve.start = blue.os.GetTime()

        self.soundNode.Play(0, 1)



    def Rest(self):
        self.dest_placement.display = 0
        self.beam_placement.display = 0
        self.source_placement.children[1].display = 0
        self.source_placement.children[2].display = 0
        self.source_placement.source = self.dummy_dest
        self.dest_placement.dest = self.dummy_dest
        self.beam_placement.dest = self.dummy_dest
        self.RemoveVertexTarget()



    def SetSubTarget(self, shipID, targetID):
        targetBall = sm.GetService('michelle').GetBall(targetID)
        if (targetBall is None):
            return 0
        if (getattr(targetBall, 'model', None) is None):
            return 0
        model = sm.GetService('damage').GetTargetLocators(shipID, targetID, 1)[0]
        self.source_placement.source = model
        self.dest_placement.dest = model
        self.beam_placement.dest = model



    def RemoveVertexTarget(self):
        if self.vxOwners:
            nodemanager.RemoveVertexTarget(self.vxOwners)
            self.vxOwners = []



    def StartShooting(self):
        blue.pyos.synchro.Sleep(1)
        self.dest_placement.display = 1
        self.beam_placement.display = 1
        self.source_placement.children[1].display = 1
        self.source_placement.children[2].display = 1




class StripMiningTurretPair():
    __persistvars__ = ['shootTimer', 'shootDelay']

    def __init__(self):
        self.shootTimer = None
        self.weaponType = None
        self.target = None
        self.shootDelay = None



    def Initialize(self, graphicID, shootDelay):
        self.shootDelay = shootDelay
        graphicURL = util.GraphicFile(graphicID)
        self.turret1 = StripMiningTurret(graphicURL)
        self.turret2 = StripMiningTurret(graphicURL)



    def Release(self):
        self.StopShooting()
        self.model = None
        self.target = None
        self.turret1.Release()
        self.turret2.Release()
        self.turret1 = None
        self.turret2 = None



    def StartShooting(self, ammoGFXid = None):
        self.turret1.StartShooting()
        self.turret2.StartShooting()
        if self.shootDelay:
            if self.shootTimer:
                self.shootTimer = None
            else:
                self.Shoot()
            offset = (((self.shootDelay / 5) * 4) + random.randint(0, (self.shootDelay / 5)))
            self.shootTimer = base.AutoTimer(offset, self.Shoot)



    def StopShooting(self):
        self.shootTimer = None



    def Rest(self):
        self.StopShooting()
        self.turret1.Rest()
        self.turret2.Rest()
        self.target = None



    def Shoot(self):
        if (self.target is None):
            sm.GetService('FxSequencer').LogError('StripMiner: Target is None, Shoot() cancelled')
            return 
        self.ShootAnimation()



    def SetTarget(self, shipID, targetID):
        self.turret1.SetTarget(shipID, targetID)
        self.turret2.SetTarget(shipID, targetID)
        targetBall = sm.GetService('michelle').GetBall(targetID)
        self.target = targetBall



    def ShootAnimation(self):
        self.shooting = 1
        blue.pyos.synchro.Sleep(random.randint(0, 100))
        if ((self.target is None) or (self.target.model is None)):
            return 
        if ((self.turret1 is None) or (self.turret1.model is None)):
            return 
        if ((self.turret2 is None) or (self.turret2.model is None)):
            return 
        dist1 = (self.turret1.model.worldPosition - self.target.model.worldPosition).Length()
        dist2 = (self.turret2.model.worldPosition - self.target.model.worldPosition).Length()
        if (dist1 < dist2):
            self.turret1.Shoot()
        else:
            self.turret2.Shoot()
        self.shooting = 0



exports = {'turret.StripMiningTurretPair': StripMiningTurretPair}

